class wxRequest{
  defaults = {
    baseURL:'',
    url:'',
    data:null,
    method:'get',
    header:{
      "Content-type":'application/json'
    },
    timeout:60000
  }

  interceptors = {
    request:(config) => config,
    response:(response) => response
  }
  // 实例化时传入的参数
  constructor(params = {}){
    this.defaults = Object.assign({},this.defaults,params)
  }
  // 封装get实例方法
  get(url,data = {},config = {}){
    return this.request(Object.assign({url,data,method:'get'},config))
  }

  // 封装post实例方法
  post(url,data = {},config = {}){
    return this.request(Object.assign({url,data,method:'post'},config))
  }

  // 封装delete实例方法
  delete(url,data = {},config = {}){
    return this.request(Object.assign({url,data,method:'delete'},config))
  }

  // 封装put实例方法
  put(url,data = {},config = {}){
    return this.request(Object.assign({url,data,method:'put'},config))
  }
  request(options) {
    options.url = this.defaults.baseURL + options.url
    options = {...this.defaults,...options}
    options = this.interceptors.request(options)
    return new Promise((resolve,reject) => {
      wx.request({
        ...options,

        success:(res) =>{
          const mergeRes = Object.assign({},res,{config:options})
          resolve(this.interceptors.response(mergeRes))
        },
        fail:(err) =>{
          const mergeErr = Object.assign({},err,{config:options})
          resolve(this.interceptors.response(mergeErr))
        }
      })
    })
  }
}

const instance = new wxRequest({
  baseURL:'https://gmall-prod.atguigu.cn/mall-api',
  timeout:15000
})

// 配置请求拦截器
instance.interceptors.request = (config) =>{

  return config
}

// 配置响应拦截器
instance.interceptors.response = (config) =>{

  return config
}
export default instance